package com.op.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.op.entity.Role;
import com.op.response.ResponseResult;
import com.op.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Role)表控制层
 *
 * @author 孤焰
 * @since 2021-10-16 22:09:53
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param role 查询实体
     * @return 所有数据（分页）
     */
    @GetMapping("/page")
    public ResponseResult<Page<Role>> page(Page<Role> page, Role role) {
        return ResponseResult.success(roleService.page(page, new QueryWrapper<>(role)));
    }

    /**
     * 查询所有数据
     *
     * @param role 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    public ResponseResult<List<Role>> selectAll(Role role) {
        return ResponseResult.success(roleService.list(new QueryWrapper<>(role)));
    }

    @GetMapping("/registerRole")
    public ResponseResult<List<Role>> registerRole() {
        return ResponseResult.success(roleService.list(new LambdaQueryWrapper<Role>().ne(Role::getRoleName, "临时用户")));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne/{id}")
    public ResponseResult<Role> selectOne(@PathVariable("id") Long id) {
        return ResponseResult.success(roleService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param role 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public ResponseResult<Boolean> insert(Role role) {
        return ResponseResult.success(roleService.save(role));
    }

    /**
     * 修改数据
     *
     * @param role 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public ResponseResult<Boolean> update(Role role) {
        return ResponseResult.success(roleService.updateById(role));
    }

    /**
     * 添加或修改数据
     * @param role 实体对象
     * @return 添加或修改结果
     */
    @PostMapping("/saveOrUpdate")
    public ResponseResult<Boolean> saveOrUpdate(Role role) {
        return ResponseResult.success(roleService.saveOrUpdate(role));
    }

    /**
     * 删除数据
     *
     * @param idList 主键集合
     * @return 删除结果
     */
    @DeleteMapping("/batchDelete")
    public ResponseResult<Boolean> batchDelete(@RequestParam("idList") List<Long> idList) {
        return ResponseResult.success(roleService.logicDelBatchById(idList));
    }
}

